<?php
// Buffer Function
function buffer($f)
{
	global $mod, $table, $sid, $id, $sPage, $sId, $block, $_GET, $_POST, $_COOKIE, $_FILES, $_SESSION;
	ob_start();               # Start Buffer
	include($f);              # Include File
	$obj = ob_get_contents(); # Store Contents
	ob_end_clean();           # Clear Buffer
	
	return $obj;              # Return Contents
}

function block($title, $content, $block, $get_id='', $details='')
{
    $getBlock = buffer('./blocks/'.$block.'.tpl');
    
    if(is_array($content))
    {
        $setContent = $content[$aId];
    }
    else
    {
        $setContent = $content;
    }
    
    $replaceContent = array(
    'id' => $get_id,
    'title' => $title,
    'content' => $content,
    'details' => $details,
    );
    
    echo replaceTag($getBlock, $replaceContent);
}

function query($q)
{
    $f = mysql_query($q)or die(mysql_error());
    return $f;
}

function between($string, $start, $eind)
{
        $string = " ".$string; 
        $ini = strpos($string,$start); 
        if ($ini == 0) return ""; 
        $ini += strlen($start); 
        $len = strpos($string,$eind,$ini) - $ini; 
        return substr($string,$ini,$len); 
}

function replaceTag($str, $replace)
{
    preg_match_all("/\{(.[^\{\}]*)\}/", $str, $out);
    
    foreach($out[1] as $key => $value)
    {
        if(isset($replace[$value]))
        {
            $str = str_replace("{".$value."}", $replace[$value], $str);
        }
    }
    
    return $str;
}

function getSections($theme)
{
    $var = buffer($theme);
    
    $pattern = '/\{section:(.*?)\}/si';
    preg_match_all($pattern, substr($var,3), $sections);
    
    return $sections[1];
}

function debugmode($i)
{
    echo '<pre>';
    print_r($i);
    echo '</pre>';
}

function v($i)
{
    return htmlspecialchars($i);
}

function cutoffMax($i,$max)
{
    // Check lenght of string
    $len = strlen($i);
    
    if($len > $max)
    {
        $cutoffAmount = $len - $max;
        $final = substr($i, 0, -$cutoffAmount).'..';
    }
    else
    {
        $final = $i;
    }
    // 12 x 18 (18/12=6 characters too much, remove last 6 characters)
    
    return $final;
}

function sql($i, $t=0)
{
	if($t == 0)
	{
		$final = mysql_real_escape_string($i);
	}
	else
	{
			if($t == 1){ $type = $_GET; }
		elseif($t == 2){ $type = $_POST; }
		elseif($t == 3){ $type = $_COOKIE; }
        elseif($t == 4){ $type = $_SESSION; }
		
        $val = isset($type[$i]) ? $type[$i] : 0;
        
		$final = mysql_real_escape_string($val);
	}
	
	return $final;
}

// String Generation
function strGen($amount, $type = 'alphanum')
{
    // Define Vars so PHP Would not Print Errors
    $build = '';
    $r     = '';
    
    // Set of characters to choose from
    $chars['alpha']    = 'abcdefghijklmnopqrstuvwxyz';
    $chars['alphaC']   = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $chars['num']      = '0123456789';
    
    // Alphanumeric if you want all
    $chars['alphanum'] = $chars['alpha'] . $chars['alphaC'] . $chars['num'];
    
    // Print out each character, I want 100 characters
    for($i = 1; $i <= $amount; $i++)
    {
        // Random Number Between 1 and amount of characters in alpha
        $r = mt_rand(0, strlen($chars[$type])-1);
        
        // Echo the random selected Character
        $build .= $chars[$type][$r];
    }
    
    // Print Build
    return $build;
}

// Easy Cookie Function
function cookie($name, $value, $time=31536000)
{
    setcookie($name, $value, time()+$time,  "/");
}

// Easy Redirect Function
function redirect($location, $time=0)
{
    echo '<META HTTP-EQUIV="REFRESH" CONTENT="'.$time.'; URL='.$location.'">';
}

function templateFiles($ads=null)
{
    if($handle = opendir('../templates'))
    {
        $trap = ($ads == null) ? array() : $ads;
        while (false !== ($file = readdir($handle)))
        {
            if($file != '.' && $file != '..')
            {
                $tmp = explode('.', $file);
                
                $trap[] .= $tmp[0];
            }
        }
    
        closedir($handle);
    }
    return $trap;
}

function blockFiles($ads=null)
{
    if($handle = opendir('../blocks'))
    {
        $trap = ($ads == null) ? array() : $ads;
        while (false !== ($file = readdir($handle)))
        {
            if($file != '.' && $file != '..')
            {
                $tmp = explode('.', $file);
                
                $trap[] .= $tmp[0];
            }
        }
    
        closedir($handle);
    }
    return $trap;
}

function getDir($dir)
{
    if($handle = opendir($dir))
    {
        $trap = array();
        while (false !== ($file = readdir($handle)))
        {
            
            if($file != '.' && $file != '..')
            {
                $tmp = explode('.', $file);
                
                $trap[] .= $tmp[0];
            }
        }
    
        closedir($handle);
    }
    return $trap;
}

function moduleFiles($ads=null)
{
    if($handle = opendir('../modules'))
    {
        $trap = ($ads == null) ? array() : $ads;
        while (false !== ($file = readdir($handle)))
        {
            
            $pos = strpos($file, 'Admin');
            
            if($file != '.' && $file != '..' && $file != 'modinit.php' && $pos === false)
            {
                $tmp = explode('.', $file);
                
                $trap[] = array(
                    0 => 'modules/'.$tmp[0].'.php',
                    1 => $tmp[0]
                );
            }
        }
    
        closedir($handle);
    }
    return $trap;
}

function getAdmins()
{
    $trap = array();
    
    $q = mysql_query("SELECT * FROM ".tAdmins)or die(mysql_error());
    while($d = mysql_fetch_object($q))
    {
        $trap[] = array(
            0 => $d->id,
            1 => $d->username
        );
    }
    
    return $trap;
}

function getPages($ads=null)
{
    $trap = array();
    
    $trap[] = array(
            0 => 0,
            1 => $ads[0]
        );
    $q = mysql_query("SELECT * FROM ".tPages)or die(mysql_error());
    while($d = mysql_fetch_object($q))
    {
        $trap[] = array(
            0 => $d->id,
            1 => $d->title
        );
    }
    
    return $trap;
}